/**
 * @file
 * @author  michal.nezerka@gmail.com
 */

#ifndef WWTILEPROVIDER_H
#define WWTILEPROVIDER_H

#include <map>
#include <wx/bitmap.h>
#include <wx/event.h>
#include <wx/filename.h>
#include <wx/msgqueue.h>
#include <wx/url.h>
#include "geoutils.h"

namespace wildwood
{

struct Tile
{
    wxBitmap *mBitmap;
};

class TileProvider; // forward declaration

class TileWorkerThread : public wxThread
{
public:
    TileWorkerThread(TileProvider &tileProvider);
    virtual ~TileWorkerThread();

    // thread execution starts here
    virtual void *Entry();

private:
    bool downloadTile(const TilePos pos);

    TileProvider &mTileProvider;
    wxMessageQueue<TilePos> mQueue;
    //wxHTTP mHttp;

    friend class TileProvider;
};

class TileProvider
{
    public:
        TileProvider(const wxString name, const wxString tpl, wxEvtHandler *eventHandler, const int eventId);
        ~TileProvider();
        wxBitmap* getTileImage(const TilePos pos);
        wxEvtHandler *mEventHandler;
        int mEventId;

    private:
        wxString mName;
        wxString mTileTemplate;
        std::map<TilePos, Tile> mTiles;
        TileWorkerThread *mWorkerThread;

        wxURL generateTileUrl(const TilePos tilePos, const wxString urlTemplate);
        wxFileName getTileDir(unsigned int zoom);
        wxFileName getTilePath(const TilePos pos);

        friend class TileWorkerThread;     // allow it to access our TileProvider
};

} // namespace

#endif // WWTILEPROVIDER_H
